using System;
using System.Collections.Generic;
using System.Text;

namespace BUSLayer
{
    public class Library
    {
        //Ngay sau n ngay (Ngay + n ngay)
        public static DateTime NgaySau_nNgay(int n)
        {
            //int[13] sn = {0,3,28,31,30,31,31,30,31,31,30,31};

            DateTime t = new DateTime();
            int ng = int.Parse(t.Day.ToString());
            int th = int.Parse(t.Month.ToString());
            int nam = int.Parse(t.Year.ToString());


            return t;
        }
        public static DateTime NgaySau_nThang(DateTime time ,int n)
        {
            DateTime t=new DateTime();
            
            int th = int.Parse(time.Month.ToString());
            int nam = int.Parse(time.Year.ToString());
            th += n;
            if (th > 12)
            {
                nam++;
            }
            t.AddMonths(th);
            t.AddYears(nam);
   
            return t;
        }
        public static bool KiemTraManNhuan(int nam)
        {
            if (nam % 400 == 0 || (nam % 4 == 0 && nam % 100 == 0))
                return true;
            return false;
        }
        public static long KhoangCachDenDauNam(DateTime ng)
        {
            long kq=0;

            int[] sn ={ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            if (KiemTraManNhuan(ng.Year) == true)
            {
                sn[2] = 29;
            }

            for (int i = 1; i <ng.Month; i++)
            {
                kq =kq+(long) sn[i];
            }
            kq += ng.Day;
            return kq;

        }
        public static long KhoangCachHaiNgay(DateTime ng1, DateTime ng2)
        {
            long kq=0;
            int day1 = ng1.Day;
            int day2 = ng2.Day;
            int th1 = ng1.Month;
            int th2 = ng2.Month;
            int n1 = ng1.Year;
            int n2 = ng2.Year;

            

            long kc1 = KhoangCachDenDauNam(ng1);
            long kc2 = KhoangCachDenDauNam(ng2);

            long khoangcachmam = n1 - n2;
            if (khoangcachmam < 0)
                khoangcachmam *= -1;
            if (n2 < n1)
            {
                for (long i = n2; i <= n1; i++)
                {
                    if (KiemTraManNhuan((int)i) == true)
                    {
                        kq += 366;
                    }
                    else
                    {
                        kq += 365;
                    }
                }
            }
            if (n1 < n2)
            {
                for (long i = n1; i <= n2; i++)
                {
                    if (KiemTraManNhuan((int)i) == true)
                    {
                        kq += 366;
                    }
                    else
                    {
                        kq += 365;
                    }
                }
            }

            long kcnga = kc1 - kc2;
            if (kcnga < 0)
                kcnga *= -1;

            kq += kcnga;
            return kq;
        }
        
    }
}
